package com.eam.asset.file.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eam.asset.file.dto.AssetHouse.AssetHouseAddDto;
import com.eam.asset.file.dto.AssetHouse.AssetHouseDto;
import com.eam.asset.file.utils.AssetEncode;
import com.eam.common.constant.Constants;
import com.eam.common.core.redis.SRTemplateCache;
import com.eam.common.utils.DateUtils;
import com.eam.common.utils.SecurityUtils;
import com.eam.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eam.asset.file.mapper.AssetHouseMapper;
import com.eam.asset.file.domain.AssetHouse;
import com.eam.asset.file.service.IAssetHouseService;

import static com.eam.asset.file.Enum.AssetEncodeEnum.ASSETHOUSE;

/**
 * 房屋资产档案Service业务层处理
 *
 * @author tzf
 * @date 2023-10-31
 */
@Service
public class AssetHouseServiceImpl extends ServiceImpl<AssetHouseMapper, AssetHouse> implements IAssetHouseService {
    //注入redisTemplate
    private final SRTemplateCache SRTemplateCache;

    //构造器注入
    @Autowired
    public AssetHouseServiceImpl(SRTemplateCache SRTemplateCache) {
        this.SRTemplateCache = SRTemplateCache;
    }

    /**
     * 查询房屋资产档案
     *
     * @param houseId 房屋资产档案主键
     * @return 房屋资产档案
     */
    @Override
    public AssetHouse selectAssetHouseByHouseId(String houseId) {
        //如果存在key,从redis中取值
        if (SRTemplateCache.hasKey(houseId)) {
            return SRTemplateCache.get(houseId, AssetHouseDto.class);
        }
        //根据ID查询
        AssetHouse assetHouse = baseMapper.selectById(houseId);
        AssetHouseDto assetHouseDto = new AssetHouseDto();
        BeanUtils.copyProperties(assetHouse, assetHouseDto);
        //存进redis
        SRTemplateCache.set(houseId, JSONUtil.toJsonStr(assetHouseDto));
        return assetHouseDto;
    }

    /**
     * 查询房屋资产档案列表
     *
     * @param assetHouse 房屋资产档案
     * @return 房屋资产档案
     */
    @Override
    public List<AssetHouse> selectAssetHouseList(AssetHouse assetHouse) {
        //todo 后续做redis缓存,考虑数据一致性,如增删改操作需要考虑
        AssetHouseDto assetHouseDto = new AssetHouseDto();
        //深拷贝
        BeanUtils.copyProperties(assetHouse, assetHouseDto);
        return baseMapper.selectAssetHouseList(assetHouseDto);
    }

    /**
     * 新增房屋资产档案
     *
     * @param assetHouse 房屋资产档案
     * @return 1 or 0
     */
    @Override
    public int insertAssetHouse(AssetHouse assetHouse) {
        //创建dto对象,深拷贝属性值
        AssetHouseAddDto assetHouseAddDto = new AssetHouseAddDto();
        BeanUtils.copyProperties(assetHouse, assetHouseAddDto);
        //设置基本的信息
        assetHouseAddDto.setCreateBy(AssetEncode.IfAbsent(SecurityUtils.getUserId()));
        assetHouseAddDto.setCreateTime(DateUtils.getNowDate());
        assetHouseAddDto.setStatus(Constants.ZERO);
        //设置资产档案编号,一级资产分类编码+二级资产分类编码+三级资产名称编码加日期时间
        StringBuffer encode = AssetEncode.getEncode(ASSETHOUSE.getType()).
                append(AssetEncode.getEncode(assetHouseAddDto.getHouseCategory())).
                append(AssetEncode.getEncodeByName(assetHouseAddDto.getHouseName())).
                append(DateUtils.dateTime());
        assetHouseAddDto.setHouseCode(encode.toString());
        return baseMapper.insert(assetHouseAddDto);
    }

    /**
     * 修改房屋资产档案
     *
     * @param assetHouse 房屋资产档案
     * @return 1 or 0
     */
    @Override
    public int updateAssetHouse(AssetHouse assetHouse) {
        assetHouse.setUpdateTime(DateUtils.getNowDate());
        assetHouse.setUpdateBy(AssetEncode.IfAbsent(SecurityUtils.getUserId()));
        AssetHouseDto assetHouseDto = new AssetHouseDto();
        BeanUtils.copyProperties(assetHouse, assetHouseDto);
        //根据id修改实体信息
        return baseMapper.updateById(assetHouseDto);
    }

    /**
     * 批量删除房屋资产档案
     *
     * @param houseIds 需要删除的房屋资产档案主键
     * @return 1 or 0
     */
    @Override
    public int deleteAssetHouseByHouseIds(String[] houseIds) {
        //批量删除根据ID
        return baseMapper.deleteBatchIds(Arrays.asList(houseIds));
    }

    /**
     * 删除房屋资产档案信息
     *
     * @param houseId 房屋资产档案主键
     * @return 1 or 0
     */
    @Override
    public int deleteAssetHouseByHouseId(String houseId) {
        return baseMapper.deleteById(houseId);
    }
}
